/* ====================================== toast  ====================================== */
import React from 'react'
import { Checkbox } from 'antd'
// ===================================================================== Select
class Index extends React.Component {
	state = {
		
	}
	
	onChange = e => {
		const { onChange, onChanged, name, isNumber, outer } = this.props
		
		let value = e.target.checked
		
		if(!outer){ this.setState({ value }) }
		
		if(isNumber){
			value = value ? 1 : 0
		}else{
			value = value ? true : false
		}
		const model = name ? { [name]: value } : value
		onChanged?.({ model, value, name } )
		onChange?.(value)
		
	}
	
	setValue = value => this.setState({ value })
	
	clear = () => this.setValue(false)
	
	render(){
		let  { size, disabled, label, children, indeter, value, isNumber } = this.props
		
		value =  this.state.value === undefined ? value : this.state.value
		
		if(isNumber && !isNaN(value)){
			value = +value
		}
		
		return (
			<Checkbox
				size			= { size || 'small' } 
				value 			= { value }
				onChange		= { this.onChange }
				checked		 	= { isNumber ? + value : value }
				indeterminate	= { indeter }
				disabled		= { disabled }
			>
				{label}
				{ children }
			</Checkbox>
		)
	}
}
export default Index